home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Util / cli / Transfer.lha / Transfer / Transfer.cc < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-08  |  10.4 KB  |  209 lines

  1. /***************************************************************************************************
  2. * Name:      Transfer - Calculate the time elapsed for transfering or riceiving a file             *
  3. * Author:    Alessandro Marinuzzi - [Alecos] - http://alecos.altervista.org/                       *
  4. * Purpose:   Simulate the transfer of a file obtaining the time elapsed                            *
  5. * Usage:     Open a shell and type "Transfer"                                                      *
  6. * Bugs:      No bug known - This program has been tested severely                                  *
  7. * Compiler:  Sas/C - GNU C++                                                                       *
  8. * Date:      08.04.2000                                                                            *
  9. * Version:   1.0                                                                                   *
  10. * Revision:  0.0                                                                                   *
  11. ***************************************************************************************************/
  12.  
  13. #include <iostream.h>
  14.  
  15. long int file_bytes;  // File that you have to transfer in Bytes
  16. long int speed_bytes;  // MegaBytes, KiloBytes or Bytes/Sec transfered
  17. int bytes_total;  // Number of Bytes to transfer each second
  18. int counter_bytes;  // Groups of Bytes for each second elapsed
  19. int file_size;  // Input - Size of the file
  20. char identifier;  // Input - Identifier (M = MegaBytes, K = KiloBytes, B = Bytes)
  21. char specifier;  // Input - Specifier (M = MegaBytes, K = KiloBytes, B = Bytes)
  22. int counter_seconds;  // Time elapsed in seconds
  23. int seconds_real;  // Real time elapsed in seconds
  24. int rest_seconds;  // Real time elapsed as rest in seconds out the minute
  25. int minutes_real;  // Real time elapsed in minutes
  26. int rest_minutes;  // Real time elapsed as rest in minutes out the hour
  27. int hours_real;  // Time elapsed in hours
  28. int rest_hours;  // Real time elapsed as rest in hours out the day
  29. int days_real;  // Real time elapsed in days
  30. char choice;  // Quit or go on with the program
  31. static const char version[] = "$VER: Transfer 1.0 (08.04.2000) By Alecos";  // Version of the program
  32.  
  33. main ()
  34.  
  35. {
  36.   // Infos about the usage
  37.   cout << "Transfer V. 1.0 - Date: 08.04.2000 - by Alessandro Marinuzzi [Alecos]\n";
  38.   cout << "This program simulates the transfer of a file giving you infos about the time elapsed\n";
  39.   while (1) {  // Starts an infinite loop
  40.     /****************************/
  41.     /*  Initialization Section  */
  42.     /****************************/
  43.     file_bytes = 0;  // Sets the variable with the value 0 for each loop
  44.     speed_bytes = 0;  // Sets the variable with the value 0 for each loop
  45.     counter_bytes = 0;  // Sets the variable with the value 0 for each loop
  46.     bytes_total = 0;  // Sets the variable with the value 0 for each loop
  47.     file_size = 0;  // Sets the variable with the value 0 for each loop
  48.     counter_seconds = 0;  // Sets the variable with the value 0 for each loop
  49.     seconds_real = 0;  // Sets the variable with the value 0 for each loop
  50.     rest_seconds = 0;  // Sets the variable with the value 0 for each loop
  51.     minutes_real = 0;  // Sets the variable with the value 0 for each loop
  52.     rest_minutes = 0;  // Sets the variable with the value 0 for each loop
  53.     hours_real = 0;  // Sets the variable with the value 0 for each loop
  54.     rest_hours = 0;  // Sets the variable with the value 0 for each loop
  55.     days_real = 0;  // Sets the variable with the value 0 for each loop
  56.     /************************/
  57.     /*  Input&Data Section  */
  58.     /************************/
  59.     // Insert and store the values
  60.     cout << "Set the transfer rate first - Bytes/Seconds\n";
  61.     cout << "Rate: must be a number bigger than 0 - Specifier: M = MegaBytes, K = KiloBytes, B = Bytes\n";
  62.     cout << "Example: rate = 5 - Specifier = K -> 5120 Bytes/second\n";
  63.     cout << "Rate: ";  // Asks for the speed
  64.     cin >> speed_bytes;  // Stores the speed
  65.     cout << "Specifier: ";  // Specify the speed (MegaBytes, KiloBytes o Bytes)
  66.     cin >> identifier;  // Stores the specifier
  67.     switch (identifier) {  // Verify if this specifier is allowed
  68.  
  69.       case 'M':
  70.       case 'm':
  71.       // Verify if we manage MegaBytes
  72.       bytes_total = speed_bytes * 1024 * 1024;  // Calculating in Bytes
  73.       // Calculating...
  74.       cout << "Transfer rate: " << bytes_total << " Bytes/second !\n";
  75.       break;
  76.  
  77.       case 'K':
  78.       case 'k':
  79.       // Verify if we manage KiloBytes
  80.       bytes_total = speed_bytes * 1024;  // Calculating in Bytes
  81.       // Calculating...
  82.       cout << "Transfer rate: " << bytes_total << " Bytes/second !\n";
  83.       break;
  84.  
  85.       case 'B':
  86.       case 'b':
  87.       // Verify if we manage Bytes
  88.       bytes_total = speed_bytes;  // Calculating in Bytes
  89.       // Calculating...
  90.       cout << "Transfer rate: " << bytes_total << " Bytes/second !\n";
  91.       break;
  92.  
  93.       default:
  94.       // If the specifier isn't M or K or B - Error !
  95.       cout << "Specifier not allowed ! - Try again !\n";  // Warning message
  96.       continue;
  97.     }
  98.     cout << "Insert the File and the Specifier (M = MegaBytes, K = KiloBytes, B = Bytes)\n";
  99.     cout << "Example: ----- File = 400 ----- Specifier = M ----- [The result is 400 MB]\n";  // Example
  100.     cout << "File: ";  // Asks for the file
  101.     cin >> file_size;  // Stores the file
  102.     cout << "Specifier: ";  // Asks for the specifier
  103.     cin >> specifier;  // Stores the specifier
  104.     switch (specifier) {  // Verify if this specifier is allowed
  105.  
  106.       case 'M':
  107.       case 'm':
  108.       // Verify if we manage MegaBytes
  109.       file_bytes = file_size * 1024 * 1024;  // Calculating in Bytes
  110.       // Calculating...
  111.       cout << "File: " << file_size << " MegaBytes = " << file_bytes << " Bytes !\n";
  112.       cout << "Please wait. I'm simulating the transfer...\n";  // Goes on with the simulation...
  113.       break;
  114.  
  115.       case 'K':
  116.       case 'k':
  117.       // Verify if we manage KiloBytes
  118.       file_bytes = file_size * 1024;  // Calculating in Bytes
  119.       // Calculating...
  120.       cout << "File: " << file_size << " KiloBytes = " << file_bytes << " Bytes !\n";
  121.       cout << "Please wait. I'm simulating the transfer...\n";  // Goes on with the simulation...
  122.       break;
  123.  
  124.       case 'B':
  125.       case 'b':
  126.       // Verify if we manage Bytes
  127.       file_bytes = file_size;  // Calculating in Bytes
  128.       // Calculating...
  129.       cout << "File: " << file_size << " Bytes = " << file_bytes << " Bytes !\n";
  130.       cout << "Please wait. I'm simulating the transfer...\n";  // Goes on with the simulation...
  131.       break;
  132.  
  133.       default:
  134.       // If the specifier isn't M or K or B - Error !
  135.       cout << "Specifier not allowed ! - Try again !\n";  // Warning message
  136.       continue;
  137.     }
  138.     /******************************/
  139.     /*  Verify&Calculate Section  */
  140.     /******************************/
  141.     if (bytes_total >= file_bytes) {  // If the file transfered is smaller than the speed - Error !
  142.       cout << "Error ! You cannot transfer a file smaller than the transfer rate !!!\n";
  143.       cout << "Try again !\n";
  144.     continue;
  145.     }
  146.     else {  // If the file transfered is bigger than the speed -  OK !
  147.       while (counter_bytes < file_bytes) {  // Starts another loop for determining the time elapsed
  148.         counter_bytes += bytes_total;  // + Bytes for each loop for reaching file_bytes
  149.         ++counter_seconds;  // Adds 1 for each loop
  150.         seconds_real = counter_seconds;  // Renames the variable for a better code
  151.         if (seconds_real >= 60) {  // If seconds_real is equal or bigger than 60
  152.           minutes_real = (seconds_real / 60);  // Obtains the minutes in integer
  153.           rest_seconds = (seconds_real % 60);  // Obtains the rest of the minutes in seconds
  154.         }
  155.         if (minutes_real >= 60) {  // If minutes_real is equal or bigger than 60
  156.           hours_real = (seconds_real / 3600);  // Obtains the hours in integer
  157.           rest_minutes = (seconds_real % 3600);  // Obtains the rest of the hours in minutes
  158.           if (rest_minutes >= 60) {  // If the rest of the hours is equal or bigger than 60
  159.             rest_minutes /= 60;  // Obtains the rest in minutes properly
  160.           }
  161.         }
  162.         if (hours_real >= 24) {  // If hours_real is equal or bigger than 24
  163.           days_real = (seconds_real / 86400);  // Obtains the days in integer
  164.           rest_hours = (seconds_real % 86400);  // Obtains the rest of the days in hours
  165.           if (rest_hours >= 24) {  // If the rest of the hours is equal or bigger than 24
  166.             rest_hours /= 3600;  // Obtains the rest in hours properly
  167.           }
  168.         }
  169.       }
  170.     }
  171.     /*********************************/
  172.     /*      Result&Print Section     */
  173.     /*********************************/
  174.     cout << "File transfered: " << file_bytes << " Bytes\n";  // Infos about the file
  175.     cout << "Time elapsed: ";  // Prepares the infos for printing
  176.     if (days_real > 0) {  // Verify if there are days for calculating the time elapsed
  177.       cout << " -> " << days_real << " days ";  // Prints the days elapsed
  178.       cout << " -> " << rest_hours << " hours ";  // Prints the hours elapsed
  179.       cout << " -> " << rest_minutes << " minutes ";  // Prints the minutes elapsed
  180.       cout << " -> " << rest_seconds << " seconds\n";  // Prints the seconds elapsed
  181.     }
  182.     if ((hours_real > 0) && (hours_real < 24)) {  // Verify if there are hours for calculating the time elapsed
  183.       cout << " -> " << hours_real << " hours ";  // Print the hours elapsed
  184.       cout << " -> " << rest_minutes << " minutes ";  // Print the minutes elapsed
  185.       cout << " -> " << rest_seconds << " seconds\n";  // Print the seconds elapsed
  186.     }
  187.     if ((minutes_real > 0) && (minutes_real < 60)) {  // Verify if there are minutes for calculating the time elapsed
  188.       cout << " -> " << minutes_real << " minutes ";  // Print the minutes elapsed
  189.       cout << " -> " << rest_seconds << " seconds\n";  // Prints the seconds elapsed
  190.     }
  191.     if ((seconds_real > 0) && (seconds_real < 60)) {  // Verify if there are seconds for calculating the time elapsed
  192.       cout << " -> " << seconds_real << " seconds\n";  // Prints the seconds elapsed
  193.     }
  194.     // Asks a choice: quit or go on !
  195.     cout << "Do you want quit ? - Press Q or q - To go on any key. Key: ";
  196.     cin >> choice;  // Stores the choice
  197.     if ((choice == 'Q') || (choice == 'q')) {  // By pressing Q or q the program stops working !
  198.       cout << "Quitted !\n";
  199.       break;
  200.     }
  201.     else {  // Goes on with the program restarting again the loop
  202.       continue;
  203.     }
  204.   }
  205.  
  206.     return (0);
  207. }
  208.  
  209.